package cn.tedu.health.controller;

import cn.tedu.health.pojo.entity.User;
import cn.tedu.health.response.JsonResult;
import cn.tedu.health.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/register")
    public JsonResult registerUser(@RequestBody User user) {
        userService.registerUser(user);
        return JsonResult.ok();
    }

    @PostMapping("/login")
    public JsonResult loginUser(@RequestBody User user) {
        User loggedInUser = userService.loginUser(user.getUsername(), user.getPassword());
        return JsonResult.ok(loggedInUser);
    }
    @PostMapping("/user")
    public JsonResult updateUser(@RequestBody User user) {
        User updatedUser = userService.updateUser(user);
        return JsonResult.ok(updatedUser);
    }

    @GetMapping("/user/{id}")
    public JsonResult getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return JsonResult.ok(user);
    }
}